package ltd.hyangyang.vegetable.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ltd.hyangyang.vegetable.entity.ClassificationNumber;
import ltd.hyangyang.vegetable.entity.History;
import ltd.hyangyang.vegetable.mapper.HistoryMapper;
import ltd.hyangyang.vegetable.service.HistoryService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HistoryServiceImpl extends ServiceImpl<HistoryMapper, History> implements HistoryService {
    @Override
    public List<ClassificationNumber> distinguishNumber() {
        List<History> histories = getBaseMapper().selectList(null);
        Map<String,Integer > distinguishNumberMap = new HashMap<>();
        histories.stream().forEach(history -> {
            Integer number = distinguishNumberMap.get(history.getClassLabel());
            if (number!=null){
                number++;
                distinguishNumberMap.put(history.getClassLabel(),number);
            }else {
                distinguishNumberMap.put(history.getClassLabel(),1);
            }
        });
        List<ClassificationNumber> classificationNumbers = new ArrayList<>();
        distinguishNumberMap.forEach((key,value)->{
            ClassificationNumber classificationNumber = new ClassificationNumber();
            classificationNumber.setName(key);
            classificationNumber.setValue(value);
            classificationNumbers.add(classificationNumber);
        });

        return classificationNumbers;
    }
}
